package com.roadjava.GDOUGym.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.extension.api.R;
import com.roadjava.GDOUGym.bean.dto.RecordDTO;
import com.roadjava.GDOUGym.bean.entity.RecordDO;
import com.roadjava.GDOUGym.bean.req.LoginReq;
import com.roadjava.GDOUGym.bean.req.RandomUtil;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.service.RecordService;
import com.roadjava.GDOUGym.service.impl.RecordServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@Slf4j
@RequestMapping("/sms")
public class SMS {
    @Resource
    private RecordService recordService;
    //添加场地
    @RequestMapping("/findSms.do") @ResponseBody
    public ResultDTO<String> findSms(@RequestBody LoginReq loginReq, HttpSession session) {

        RecordDTO dto = new RecordDTO();
        dto.setTelephone(loginReq.getPhone());
        System.out.println(dto.getTelephone());
        dto.setPageNow(1);
        dto.setPageSize(10);
        ResultDTO<List<RecordDTO>>  listResultDTO = recordService.find(dto);
        System.out.println(listResultDTO);
        if(listResultDTO.getData().size()== 0){
            return ResultDTO.buildSuccess("查无此手机号，请注册");
        }

        //2 如果redis获取 不到，进行阿里云发送
        //生成随机值，传递阿里云进行发送
        String code = RandomUtil.getFourBitRandom();
        Map<String, Object> param = new HashMap<>();
        param.put("code", code);
        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAI5tR9m1d1hZAXeDzqKNxX", "W3L3zbHjVAK81sqxMLNX2QPqNF06Te");
        IAcsClient client = new DefaultAcsClient(profile);

        //设置相关固定的参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //设置发送相关的参数
        request.putQueryParameter("PhoneNumbers", loginReq.getPhone()); //手机号
        request.putQueryParameter("SignName", "吴坪峰的博客"); //申请阿里云 签名名称
        request.putQueryParameter("TemplateCode", "SMS_275380647"); //申请阿里云 模板code
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据，转换json数据传递

        try {
            //最终发送
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            if (success) {
                System.out.println("成功发送");
                RecordDO dos = listResultDTO.getData().get(0);
                session.setAttribute("login",dos);
                return ResultDTO.buildSuccess(code);
            } else {
                System.out.println("发送失败");
                ResultDTO<String> R = new ResultDTO<>();
                R.setSuccess(false);
                return ResultDTO.buildSuccess("发送失败，请检查手机号或网络");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发送失败");
        }
        ResultDTO<String> R = new ResultDTO<>();
        R.setSuccess(false);
        return ResultDTO.buildSuccess("发送失败，请检查手机号或网络");
    }

}
